<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FolderController
 *
 * @author QuangKhoi
 */
class FolderController {
    //put your code here

    /**
     * Store model object
     * @var Folder
     */
    private $model;

    /**
     * Store view for the folder
     * @var FolderView
     */
    private $view;

    /**
     * Default constructor
     */
    public function __construct($model, $view) {
        $this->model = $model;
        $this->view = $view;
    }

    /**
     * Update folder infos or add new folder to library
     */
    public function save() {
        $model = $this->model;
        $query = "SELECT * FROM folders WHERE folderid = $model->id";
        $result = Database::getInstance()->query($query);
        if ($result->returnCode == 200)  //OK
            if ($result->dataTable->numOfRow > 0) {
                Database::getInstance()->query("UPDATE folders
                        SET foldername='$model->folderName', 
                        userid='$model->userId', 
                        foldertype='$model->folderType', 
                        parentid='$model->parentId' 
                        WHERE folderid = $model->folderId");
                return true;
            } else {
                Database::getInstance()->query("INSERT INTO folders
                            (userid, foldername, foldertype, parentid) VALUES
                            ('$model->userId', '$model->folderName', 
                            '$model->folderType', '$model->parentId')");
                return true;
            }
    }

    /**
     * Delete this folder
     */
    public function delete() {
        $model = $this->model;
        $query = "DELETE FROM folders WHERE folderid = $model->folderId OR parentid = $model->folderId";
        Database::getInstance()->query($query);
        Database::getInstance()->query("DELETE FROM streams WHERE folderid = $model->folderId OR folderid IN (SELECT folderid FROM folders WHERE parentid = $model->folderId)");
        return true;
    }

    /**
     * 
     * @param Int $folderId
     */
    public static function loadStructure($folderId) {
        global $userId;
        $db = Database::getInstance();
        $user = User::getLogedInUser();
        if ($folderId > 0) {
            $folder = Folder::withId($folderId);
            $query = "SELECT * FROM folders WHERE parentid = '$folderId' AND userid IN (SELECT userid FROM users WHERE teamid = (SELECT teamid FROM users WHERE userid = $userId)) ORDER BY folderid";
            if ($user->type == 3 && ($user->teamId == 7 || $user->teamId == 3)) //Unilever & SAMSUNG TEAM
                $query = "SELECT * FROM folders LEFT JOIN foldershare ON folders.folderid = foldershare.folderid WHERE parentid='$folderId' AND foldershare.sharewith = $userId AND folders.parentid = '$folderId' AND folders.userid IN (SELECT userid FROM users WHERE teamid = (SELECT teamid FROM users WHERE userid = $userId)) ORDER BY folders.folderid";
            $result = $db->query($query);
            if ($result->returnCode == 200) { //OK
                if ($result->dataTable->numOfRow > 0) {
                    $folder->child = array();
                    foreach ($result->dataTable->rows as $row)
                        $folder->child[] = self::loadStructure($row["folderid"]);
                }
            }
        } else {
            $folder = array();
            $query = "SELECT * FROM folders WHERE parentid = '0' AND userid IN (SELECT userid FROM users WHERE teamid = (SELECT teamid FROM users WHERE userid = $userId)) ORDER BY folderid";
            if ($user->type == 3 && ($user->teamId == 7 || $user->teamId == 3)) //Unilever TEAM
                $query = "SELECT * FROM folders LEFT JOIN foldershare ON folders.folderid = foldershare.folderid WHERE foldershare.sharewith = $userId AND folders.parentid = '$folderId' AND folders.userid IN (SELECT userid FROM users WHERE teamid = (SELECT teamid FROM users WHERE userid = $userId)) ORDER BY folders.folderid";
            $result = $db->query($query);
            if ($result->returnCode == 200) { //OK
                if ($result->dataTable->numOfRow > 0) {
                    $rows = $result->dataTable->rows;
                    foreach ($rows as $row)
                        $folder[] = self::loadStructure($row["folderid"]);
                }
            }
        }
        return $folder;
    }

    public function render() {
        $folderTypes = array();
        $folders = self::loadStructure(0);
        $this->view->folders = $folders;
        $masterView = MasterView::getInstance();
        $masterView->folderView = $this->view;
    }

}

?>
